include('shared.lua')
include('cl_viewscreen.lua')

SWEP.PrintName          = "Stargate GDO"
SWEP.Slot               = 4
SWEP.Slotpos            = 1
SWEP.Drawammo           = false
SWEP.Drawcrosshair      = false
SWEP.WepSelectIcon = surface.GetTextureID("weapons/gdo_inventory1")

CreateClientConVar("cl_weapon_gdo_iriscode", 0, true, true)
CreateClientConVar("cl_weapon_gdo_codelist", "", true, true)

function ShowIrisMenu()

	local codesString = LocalPlayer():GetInfo("cl_weapon_gdo_codelist")
	codes = {}
	
	if codeString != "" then
		local tempvar = ""
		for k,v in pairs(string.Explode("|", codesString)) do
			tempvar = string.Explode(",", v)
			codes[tempvar[1]] = tempvar[2]
		end
	end
	
	local panel = vgui.Create("DFrame")
	panel:SetDeleteOnClose()
	panel:ShowCloseButton(false)
	panel:SetTitle("-(SF)- Select the iris code")
	panel:SetSize(350, 215)
	panel:SetPos((ScrW()/2 - 350) / 2, ScrH()/2 - 75)
	panel:MakePopup()
	
	local codeLabel = vgui.Create("DLabel", panel)
	codeLabel:SetPos(10,25)
	codeLabel:SetText("Iris Code")
	
	local descLabel = vgui.Create("DLabel", panel)
	descLabel:SetPos(120,25)
	descLabel:SetText("Description")
	
	local code = vgui.Create( "DTextEntry", panel)
	code:SetPos( 10, 45)
	code:SetSize(100, 20)	
	
	local desc = vgui.Create ("DTextEntry", panel)
	desc:SetPos(120, 45)
	desc:SetSize(100, 20)
	
	for k,v in pairs(codes) do
		if v == LocalPlayer():GetInfo("cl_weapon_gdo_iriscode") then
			code:SetText(v)
			desc:SetText(k)
		end
	end
	
	local addButton = vgui.Create("DButton", panel)
	addButton:SetPos(230, 45)
	addButton:SetSize(20,20)
	addButton:SetText("+")
	
	function addButton:DoClick()
		local found = false
		for k,v in pairs(codes) do
			if v == tonumber(code:GetValue()) or k == desc:GetValue() then
				found = true
			end
		end

		if not found then
			codes[string.gsub(string.gsub(desc:GetValue(), "|", ""), ",", "")] = tonumber(code:GetValue())
			updateCodes()
		end
	end
	
	local remButton = vgui.Create("DButton", panel)
	remButton:SetPos(260, 45)
	remButton:SetSize(20,20)
	remButton:SetText("-")
	
	function remButton:DoClick()
		local found = false
		for k,v in pairs(codes) do
			if v == tonumber(code:GetValue()) or k == desc:GetValue() then
				found = true
				codes[k] = nil
			end
		end
		
		if found then
			updateCodes()
		end
	end
	
	local codeList = vgui.Create( "DListView", panel)
	codeList:SetPos(10, 75)
	codeList:SetSize(330, 100)
	codeList:AddColumn("Code")
	codeList:AddColumn("Description")
	codeList:SortByColumn(1, true)
	
	function updateCodes()
		codeList:Clear()
		for k,v in pairs(codes) do
			codeList:AddLine(v, k)
		end
	end
	
	updateCodes()
	
	function codeList:OnRowSelected(id, selected)
		local codeS = selected:GetColumnText(1)
		local descS = selected:GetColumnText(2)
		code:SetText(codeS)
		desc:SetText(descS)
	end
	
	function codeList:DoDoubleClick(ListView, List)
		saveCodes()
		panel:Close()
	end
	
	local saveClose = vgui.Create("DButton", panel)
	saveClose:SetText("Save and Close")
	saveClose:SetSize(100, 22)
	saveClose:SetPos(10,185)
	
	function saveClose:DoClick()
		saveCodes()
		panel:Close()
	end
	
	function saveCodes()
		addButton:DoClick()
		local codesList = {}
		LocalPlayer():ConCommand("cl_weapon_gdo_iriscode " .. tonumber(code:GetValue()) .. "\n")
		for k,v in pairs(codes) do
			table.insert(codesList, k..","..v)
		end
		LocalPlayer():ConCommand("cl_weapon_gdo_codelist " .. string.Implode("|", codesList) .. "\n")
	end
	
	local cancelButton = vgui.Create("DButton", panel)
	cancelButton:SetText("Cancel")
	cancelButton:SetPos(120, 185)
	
	function cancelButton:DoClick()
		panel:Close()
	end
end

function gdo_menuhook(um)
	ShowIrisMenu()
end

usermessage.Hook("gdo_openpanel", gdo_menuhook)